using Gateway.Common.Enums;
using Gateway.IApplication.BaseInput;
using Gateway.IApplication.Menu;
using Gateway.IApplication.Menu.Input;
using Gateway.IQuery.Menu;
using Gateway.IQuery.Menu.Result;
using Gateway.UI.Api.Filters.Attributes;

namespace Gateway.UI.Api.Controllers;

/// <summary>
/// 菜单/权限服务
/// </summary>
public class MenuController : BaseController
{
    private readonly IMenuApp _menuApp;
    private readonly IMenuQuery _menuQuery;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="menuApp"></param>
    /// <param name="menuQuery"></param>
    public MenuController(IMenuApp menuApp, IMenuQuery menuQuery)
    {
        _menuApp = menuApp;
        _menuQuery = menuQuery;
    }

    /// <summary>
    /// 创建菜单/权限
    /// </summary>
    /// <param name="input"></param>
    /// <returns>返回主键标识</returns>
    [HttpPost, Route("add"),Log(logType: LogTypeEnum.MenuOperate,logEvent:"创建了菜单/权限，菜单Code:${Name}")]
    public async Task<string> Add(CreateMenuInput input)
    {
        return await _menuApp.Add(input);
    }

    /// <summary>
    /// 更新菜单/权限
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("update"),Log(logType: LogTypeEnum.MenuOperate,logEvent:"更新了菜单/权限，菜单标识:${MenuId}")]
    public async Task<bool> Update(UpdateMenuInput input)
    {
        return await _menuApp.Update(input);
    }

    /// <summary>
    /// 删除菜单/权限
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("remove"),Log(logType: LogTypeEnum.MenuOperate,logEvent:"删除了菜单/权限，菜单标识:${Id}")]
    public async Task<bool> Remove(RemoveInput input)
    {
        return await _menuApp.Remove(input.Id);
    }
    /// <summary>
    /// 删除菜单/权限 -批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpPost, Route("remove/batch"),Log(logType: LogTypeEnum.MenuOperate)]
    public async Task<bool> BatchRemove(List<string> ids)
    {
        foreach (var item in ids)
        {
            await _menuApp.Remove(item);
        }

        return true;
    }
    /// <summary>
    /// 获取菜单详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet, Route("detail/{id}")]
    public async Task<MenuDetailResult> GetDetail(string id)
    {
        return await _menuQuery.GetDetail(id);
    }

    /// <summary>
    /// 获取树形菜单
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("tree")]
    public async Task<List<MenuTreeResult>> GetTree()
    {
        return await _menuQuery.GetTree();
    }
    
    /// <summary>
    /// 改变 level 
    /// </summary>
    /// <returns></returns>
    [HttpPost, Route("change/level")]
    public async Task<bool> ChangeLevel(string draggingNodeId, string dropNodeId, string dropType)
    {
        return await _menuApp.ChangeLevel(draggingNodeId, dropNodeId, dropType);
    }
}